package indi.zhifa.engine.starter.redis;

import com.alibaba.fastjson2.support.config.FastJsonConfig;
import com.alibaba.fastjson2.support.spring6.data.redis.FastJsonRedisSerializer;
import indi.zhifa.engine.redis.properties.NormalRedisProperties;
import indi.zhifa.engine.redis.properties.RedisConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@RequiredArgsConstructor
@AutoConfiguration
@ConditionalOnProperty(prefix = "zhifa.redis.normal",name = "enabled", havingValue = "true")
public class NormalRedisAutoConfiguration {

    @ConfigurationProperties(prefix = "zhifa.redis.normal")
    @Bean(name = "normal-redis-properties")
    public NormalRedisProperties redisProperties(){
        return new NormalRedisProperties();
    }

    /**
     * springBoot的配置很复杂， 配一个简单的，如果需要大家自行配一个
     *
     */
    @Primary
    @Bean(name = "normal-redis-connection-factory")
    public RedisConnectionFactory redisConnectionFactory(@Qualifier(value = "normal-redis-properties") NormalRedisProperties pRedisProperties){
        return RedisConfig.redisConnectionFactory(pRedisProperties);
    }

    @Bean(name = "default-redis-json-serializer")
    public FastJsonRedisSerializer fastJsonRedisSerializer(@Qualifier(value = "serialize-fastJson-config") FastJsonConfig pFastJsonConfig) {
        FastJsonRedisSerializer fastJsonRedisSerializer = new FastJsonRedisSerializer(Object.class);
        fastJsonRedisSerializer.setFastJsonConfig(pFastJsonConfig);
        return fastJsonRedisSerializer;
    }

    @Primary
    @Bean(name = "normal-redis-template")
    public RedisTemplate<String, Object> redisTemplate(@Qualifier(value = "normal-redis-connection-factory") RedisConnectionFactory factory,
                                                       @Qualifier(value = "default-redis-json-serializer") FastJsonRedisSerializer pFastJsonRedisSerializer) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(pFastJsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(pFastJsonRedisSerializer);
        return redisTemplate;
    }

}
